package com.howso.cloud.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.howso.cloud.common.constant.SecurityConstants;
import com.howso.cloud.feign.UserService;
import com.howso.cloud.oauth2.model.SysRole;
import com.howso.cloud.oauth2.model.SysUser;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 获取当前登录人工具类
 *
 */
public class LoginUserUtils {
    /**
     * 获取当前登录人
     */
    public static SysUser getCurrentUser(HttpServletRequest request, boolean isFull) {
        SysUser user = null;

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && !(authentication instanceof AnonymousAuthenticationToken)) {
            Object principal = authentication.getPrincipal();
            //客户端模式只返回一个clientId
            if (principal instanceof SysUser) {
                user = (SysUser)principal;
            }
        }
        if (user == null) {
            String userId = request.getHeader(SecurityConstants.USER_ID_HEADER);
            String loginString=request.getHeader(SecurityConstants.USER_HEADER);
            String roles = request.getHeader(SecurityConstants.ROLE_HEADER);

            if (StrUtil.isAllNotBlank( userId)) {
                if (isFull) {
                    UserService userService = SpringUtil.getBean(UserService.class);
                    if(loginString.startsWith("password_code:")){
                        user=userService.selectByUsername(loginString.replace("password_code:",""));
                    }
                    if(loginString.startsWith("email_password:")){
                        user=userService.findByEmail(loginString.replace("email_password:",""));
                    }
                    if(loginString.startsWith("idCard_password:")){
                        user=userService.findByIdCard(loginString.replace("idCard_password:",""));
                    }
                    if(loginString.startsWith("mobile_password:")){
                        user=userService.findByMobile(loginString.replace("mobile_password:",""));
                    }
                    if(loginString.startsWith("mobile_sms:")){
                        user=userService.findByMobile(loginString.replace("mobile_sms:",""));
                    }
                    if(loginString.startsWith("openId:")){
                        user=userService.findByOpenId(loginString.replace("openId:",""));
                    }
                    if(loginString.startsWith("mobile_sms:")){
                        user=userService.findByMobile(loginString.replace("mobile_sms:",""));
                    }
                } else {
                    user = new SysUser();
                    user.setId(Long.valueOf(userId));
                }
                if (StrUtil.isNotBlank(roles)) {
                    List<SysRole> sysRoleList = new ArrayList<>();
                    Arrays.stream(roles.split(",")).forEach(role -> {
                        SysRole sysRole = new SysRole();
                        sysRole.setCode(role);
                        sysRoleList.add(sysRole);
                    });
                    user.setRoles(sysRoleList);
                }
            }
        }
        return user;
    }
}
